﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using LowLevelGraphics.Extractors;
using LowLevelGraphics;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// Use this to auto crop a picture
    /// </summary>
    public class ActionAutoCrop : AbstractCommand
    {
        protected bool m_bPreview = true;

        protected AUTOCROP_MODE m_MODE = AUTOCROP_MODE.RGBFILTER_METHOD;

        /// <summary>
        /// Initializes a new instance of the <see cref="ActionAutoCrop"/> class.
        /// </summary>
        /// <param name="_bPreview">if set to <c>true</c> [_b preview].</param>
        public ActionAutoCrop(bool _bPreview)
        {
            m_bPreview = _bPreview;
            m_MODE = AUTOCROP_MODE.RGBFILTER_METHOD;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="_bPreview"></param>
        /// <param name="_MODE"></param>
        public ActionAutoCrop(bool _bPreview, AUTOCROP_MODE _MODE)
            : this(_bPreview)
        {
            m_MODE = _MODE;
        }

        /// <summary>
        /// MODE
        /// </summary>
        public AUTOCROP_MODE MODE
        {
            get { return m_MODE; }
            set { m_MODE = value; }
        }

        /// <summary>
        /// Runs the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override void Run(object sender, EventArgs e)
        {
            AutoCrop autoCrop = new AutoCrop();
            autoCrop.AUTOCROP_MODE = m_MODE;
            autoCrop.DrawRectangle = m_bPreview;
            UnsafeBitmap bitmap = autoCrop.Crop(MainFormSingleton.ActivePictureBox.BackgroundImage);
            bitmap.Dispose();
            MainFormSingleton.ActivePictureBox.BackgroundImage = bitmap.InternalBitmap;
            bitmap.Dispose();
        }
    }
}
